package selenium;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class wangyihao {
//网易基本成型            帐号文件为E:\\帐号/娱乐号.txt
	public static void main(String[] args) throws IOException {
		//**************唯一更改****************
		String filepath = "E:\\采集王";// 视频文件夹路径I:\未删选
		String zhangHaoLeixing = "娱乐号";//文件夹和文件名都是这个
		String titleBetter = "标题优化";//标题优化文件词汇库
		String descriptionBetter = "描述优化";//描述优化文件词汇库
		
		////////////重大说明
		//帐号文件路径为E:\\帐号/娱乐号.txt
		////////////////////////////////
		int dengdai = 50;//等待视频上传时间长度
		int katushijian = 20;//视频100%到网易返回图片之间的时间差
		int tanchuAlert = 8;//点击提交按钮后8秒时间内如果不弹出窗口，代表有问题，直接关闭当前页面，防止崩溃
		int buShuaXin = 6; //检测不刷新的次数，页面刷新6次还不出结果，就放行，让浏览器崩溃
		int pageChaoShi = 3;//设置超时时间为3S，页面等待3秒时间还没加载进来就采取行动
		int dengDaiShiMiao = 6000;//设置超时时间为6S，页面等待6秒时间还没加载进来就采取行动
		int tanChuangTiaoGuo = 3000;//设置超时时间为3S，alert().accept()
		int zuoPage = 5;//设置超时时间为3S,左边页面3秒不出来，进行循环刷新
		
		String[] leimu ={"电视剧","电影"};
		List<String> leimuList = new ArrayList<String>(Arrays.asList(leimu));
		Collections.shuffle(leimuList);
		//final String leimu2 ="电视剧片段";
		
		
		String[] tags = {"剧本","短片","电影","电视剧","电影片段","电视剧片段","影视剧","影视","片段","微电影","片花预告","解说盘点","网剧"};
		//**************唯一更改****************
		Pattern patPunc = Pattern.compile("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]$");
		//第几张图片
		String[] dianjitupian = { "//*[@id='auto']/ul/li[3]", "//*[@id='auto']/ul/li[4]", "//*[@id='auto']/ul/li[5]",
				"//*[@id='auto']/ul/li[6]", "//*[@id='auto']/ul/li[7]", "//*[@id='auto']/ul/li[8]",
				"//*[@id='auto']/ul/li[9]", "//*[@id='auto']/ul/li[10]" };
		String zhangHaofilepath = "E:\\帐号/"+zhangHaoLeixing;// 帐号文件夹路径
		String zhangHaofileSource = "E:\\帐号/";// 源帐号文件所在位置
		
		SimpleDateFormat format0 = new SimpleDateFormat("HH_mm_ss");
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
		Date currentTime = new Date();
		String dateString = format1.format(currentTime);
		
		Random random = new Random();
		String filepathqiege = new String (filepath);
		
		//**************生成帐号文件类型文件夹****************
		File zhangHaofile = new File(zhangHaofilepath);
		if (!zhangHaofile.exists()) {
			zhangHaofile.mkdirs();
		}
		//**************生成帐号文件类型文件夹****************
		
		//**************生成随机帐号文件列表****************
		File zhangHaoExist = new File(zhangHaofilepath + "/" + dateString + ".txt");// 读视频文件
		if (!zhangHaoExist.exists()) {
			List<String> listFile = readFileAll(zhangHaofileSource + zhangHaoLeixing + ".txt");
			Collections.shuffle(listFile);
			writeFile(zhangHaofilepath + "/"+dateString+".txt", listFile);
		}
		//**************生成随机帐号文件列表****************
		
		//**************标题优化和描述优化****************
		List<String> listTitleBetter = readFileAll(
				zhangHaofileSource + "/" + zhangHaoLeixing + "/" + titleBetter + ".txt");
		List<String> listdescriptionBetter = readFileAll(
				zhangHaofileSource + "/" + zhangHaoLeixing + "/" + descriptionBetter + ".txt");
		//**************标题优化和描述优化****************
		
		
		
		//**************生成随机视频文件列表列表****************
		File shiPingAExist = new File(filepath + "/顺序.txt");// 读视频文件
		if (!shiPingAExist.exists()) {
			File fileExist = new File(filepath);
			String[] filelist = fileExist.list();
			for (int i = 0; i < filelist.length; i++) {
				String fileName = filelist[i];
				String endWith123 = fileName.split(".mp4")[0];
				if ((!fileName.endsWith(".mp4")) || endWith123.endsWith(")")) {
					continue;
				}
				method2(filepath + "/顺序" + ".txt", filepath + "/" + fileName);
			}
			List<String> listFile = readFileAll(filepath + "/"+ "顺序" + ".txt");
			Collections.shuffle(listFile);
			writeFile(filepath + "/乱序.txt", listFile);
		}
		//**************生成随机视频文件列表列表****************
		
		
		
		//**************检测帐号是不是已经全部发送完和视频是不是已经没有了****************
		List<String> listZhangHaoCount = readFileAll(zhangHaofilepath + "/" + dateString + ".txt");
		List<String> listShiPingCount = readFileAll(filepath + "/"+ "乱序" + ".txt");
		if(listZhangHaoCount.size()<1||listShiPingCount.size()<1){
			System.out.println("◆◆◆◆◆◆◆◆◆◆◆");
			System.out.println("账号数量还有【"+listZhangHaoCount.size()+"】");
			System.out.println("视频数量还有【"+listShiPingCount.size()+"】");
			System.out.println("帐号或者视频没有了，请马上处理！");
			System.out.println("◆◆◆◆◆◆◆◆◆◆◆");
			throw new RuntimeException();
		}
		//**************检测帐号是不是已经全部发送完和视频是不是已经没有了****************
		
		Runtime.getRuntime().exec("taskkill /F /IM chrome.exe");
		Runtime.getRuntime().exec("taskkill /F /IM chromedriver.exe");
		 
		final WebDriver driver = new ChromeDriver();
		Point targetPosition = new Point(500,300);
		driver.manage().window().setPosition(targetPosition );
		//driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
		try {
			for (int bengkui = 0; bengkui < 9999999; bengkui++) {
				for (int i = 0; i < buShuaXin; i++) {
						driver.manage().deleteAllCookies();
				        try {
				        	//设置超时时间为3S
							driver.manage().timeouts().pageLoadTimeout(pageChaoShi, TimeUnit.SECONDS);
							driver.get("http://mp.163.com/wemedia/baseinfo/isexist/5");
							Thread.sleep(300);
							if(driver.getTitle().contains("登录")){
								break;
							}
						} catch (Exception e) {
							continue;
						}
				}
				for (int z = 0; z < 99999; z++) {
					System.gc();
				driver.manage().deleteAllCookies();

				SimpleDateFormat sdf = new SimpleDateFormat("M/dd/yyyy hh:mm:ss a",Locale.US);
				
				String name = "NTES_SESS";
				// 读取帐号文件
				File filename2 = new File(zhangHaofilepath + "/" + dateString + ".txt"); // 读帐号文件
				InputStreamReader reader2 = new InputStreamReader(new FileInputStream(filename2)); 
				BufferedReader br2 = new BufferedReader(reader2);
				String line2 = "";
				line2 = br2.readLine().trim();
				//List<String> listZhangHaoTip = readFile(zhangHaofilepath + "/" + dateString + ".txt", line2);
				String[] s1 = line2.split("\\/", -1);
				String value = s1[3];
				System.out.println("---------------start---------------------");
				System.out.println(s1[0]+"。开始执行上传任务");
				System.out.println("读入帐号成功：" + value);
				
				String domain = ".mp.163.com";
				String path = "/";
				Date expiry = sdf.parse("5/13/2108 10:31:37 AM");
				Cookie cookie = new Cookie(name, value, domain, path, expiry);
				driver.manage().addCookie(cookie);	
				Thread.sleep(400);
				for (int i = 0; i < buShuaXin; i++) {
					System.out.println("登录成功后进入收入页面,刷新次数【"+i+"】");
						try {
							// 设置超时时间为3S
							driver.manage().timeouts().pageLoadTimeout(pageChaoShi, TimeUnit.SECONDS);
							driver.get("http://mp.163.com/index.html#/income-stat");
							Thread.sleep(300);
							String shouyiTitle = driver.getTitle();
							if(shouyiTitle.contains("收益统计")){
								break;
							}
						} catch (Exception e) {
							continue;
						}
				        System.out.println("读收益监测点");
				}
				System.out.println("验证点99999");
				Thread.sleep(200);
				WebElement ddElement1 = driver.findElement(By.xpath("//*[@class='column column-5']/div/ul/li[2]/a"));
				String idString = ddElement1.getAttribute("href").toString().replace("http://mp.163.com/article/postpage/",
						"");
					String videoUrl = "http://mp.163.com/video/postpage/" + idString;
				for (int y = 0; y < 6; y++) {
					driver.findElement(By.linkText("发布")).click();
					System.gc();
					//**************刷出左边列表****************
					for (int i = 0; i < buShuaXin; i++) {
						System.out.println("刷出左边列表,刷新次数【"+i+"】");
						try {
							if (i>0) {
								driver.navigate().refresh();
							}
							//设置超时时间为3S
					        driver.manage().timeouts().pageLoadTimeout(pageChaoShi, TimeUnit.SECONDS);
							driver.get(videoUrl + "&fb=" + random.nextInt(9999));
							System.out.println("第【"+i+"】次发送视频页面请求成功");
							System.out.println("发送视频页面已经进入【等待验证函数】内部");
							new WebDriverWait(driver, zuoPage).until(new ExpectedCondition<WebElement>() {
								@Override
								public WebElement apply(WebDriver d) {
									System.out.println("抓取【左边列表验证点】开始");
									WebElement ddElement = driver.findElement(By.className("ne-icon-setting"));
										if (ddElement != null) {
											System.out.println("刷出左边列表,成功找到了左边的验证点");
											return ddElement;
										} else {
											System.out.println("刷出左边列表,刷新为null值");
											return null;
										}
								}
							});
							i = buShuaXin;
						} catch (Exception e) {
							System.out.println("循环卡在【左边列表没出来】,刷左边列表在本次循环中出现问题了");
							continue;
						}
					}
					//**************刷出左边列表****************
					
					// 读取视频地址
					File filename1 = new File(filepath + "/"+ "乱序" + ".txt");
					InputStreamReader reader = new InputStreamReader(new FileInputStream(filename1));
					BufferedReader br = new BufferedReader(reader); 
					String line0 = "";
					line0 = br.readLine().trim();
					String shouzimu = filepathqiege.substring(0,1);
					String zhuanhuan = line0.substring(1);
					String line1 = new String(shouzimu+zhuanhuan);
					String[] line11 = line1.split("●");
					String lineMp4 = line11[0];
					System.out.println("读入视频文件成功：" + lineMp4);
					String yanzheng123 = lineMp4.split(".mp4")[0].trim();
					File lineMp4Exist = new File(lineMp4);
					if (!lineMp4.endsWith(".mp4") || yanzheng123.endsWith(")")||!lineMp4Exist.exists()) {//视频不存在也删除
						// 删除第一行
						List<String> listFile = readFile(filepath + "/"+ "乱序" + ".txt", line0);
						writeFile(filepath + "/"+ "乱序" + ".txt", listFile);
						continue;
					}
					
					WebElement maxCount = driver.findElement(By.xpath("//*[@id='mainContent']/div[1]/span[1]/span"));
					String iMaxCount = maxCount.getText();
					System.out.println(iMaxCount);
					String todayMax = iMaxCount.split("\\/")[1];
					String s = iMaxCount.split("\\/")[0];
					String todayAready = s.split("：")[1];
					y = Integer.parseInt(todayAready);
					int yMax = Integer.parseInt(todayMax);
					System.out.println("正在处理第【" + (y + 1) +"】个视频");
					if(y>=yMax){
						y=7;//大于7才能停止循环	
						for (int i = 0; i < buShuaXin; i++) {
							System.out.println("发布视频进入页面,刷新次数【"+i+"】");
								try {
									// 设置超时时间为3S
									driver.manage().timeouts().pageLoadTimeout(pageChaoShi, TimeUnit.SECONDS);
									driver.get("http://mp.163.com/index.html#/income-stat");
									Thread.sleep(300);
									String shouyiTitle = driver.getTitle();
									if(shouyiTitle.contains("收益统计")){
										break;
									}
								} catch (Exception e) {
									continue;
								}
						}
						continue;
					}
					driver.findElement(By.id("video-up-input")).sendKeys(lineMp4);
					driver.findElement(By.xpath("//form[@id='videoPost']/div[2]/div[6]/div/ul/li[1]/a/label")).click();
					//标题和复制标题开始,,,,最后一段才是真正的精华！
					String valueFileTitle99 = driver.findElement(By.id("fileTitle")).getAttribute("value");
					String valueFileTitle=valueFileTitle99.replace("…", "。");
					//----------解决超过27字符和小于11字符---开始
							String valueFileTitleNew = new String(valueFileTitle);
							String numberLength = driver.findElement(By.className("j-checkTitle")).getText();
							int intNumberMax = Integer.parseInt(numberLength);
						if (intNumberMax < 23) {
							String titleMore = listTitleBetter.get(random.nextInt(listTitleBetter.size() - 1)).toString();
							Matcher matcher = patPunc.matcher(valueFileTitleNew);
							if (matcher.find()) {
								valueFileTitleNew = new String(valueFileTitleNew + titleMore.replace("\r\n", ""));
							}else {
								valueFileTitleNew = new String(valueFileTitleNew +"，"+ titleMore.replace("\r\n", ""));
							}
							driver.findElement(By.id("fileTitle")).clear();
							driver.findElement(By.id("fileTitle")).sendKeys(valueFileTitleNew);
						} else {
							driver.findElement(By.id("fileTitle")).clear();
							driver.findElement(By.id("fileTitle")).sendKeys(valueFileTitleNew);
						}
					//----------解决超过27字符和小于11字符---结束
							
					String description = new String(valueFileTitle);
					String descriptionBetterString = "";
					if (listdescriptionBetter.size()>0) {
						descriptionBetterString = listdescriptionBetter.get(random.nextInt(listdescriptionBetter.size() - 1)).toString();
					}
					driver.findElement(By.id("description")).sendKeys(description + " " + descriptionBetterString);//descriptionBetter
					
					//贴标签开始
					List <String> listTags = java.util.Arrays.asList(tags);
					Collections.shuffle(listTags);
					
					//------------------贴标签开始------------------//
					Actions action = new Actions(driver);
					for (int i = 0; i < 5; i++) {
						driver.findElement(By.id("form-tags-input")).sendKeys(listTags.get(i));
						action.sendKeys(Keys.SPACE).perform();
					}
					//------------------贴标签结束------------------//
					
					String leimufuzhi = leimuList.get(random.nextInt(leimuList.size()));
					String leimu1 = new String(leimufuzhi);
					driver.findElement(By.xpath("//*[@id='videoPost']/div[2]/div[4]/div/div[1]/button")).click();
					driver.findElement(By.xpath("//*[@id='videoPost']/div[2]/div[4]/div/div[1]/div/div/input")).sendKeys(leimu1);
					Thread.sleep(50);
					driver.findElement(By.linkText(leimu1)).click();
					/*//弹起电视剧片段
					driver.findElement(By.xpath("//*[@id='videoPost']/div[2]/div[4]/div/div[2]/button")).click();
					try {
						(new WebDriverWait(driver, 3)).until(new ExpectedCondition<WebElement>() {
							@Override
							public WebElement apply(WebDriver arg0) {
								//*[@id="videoPost"]/div[2]/div[4]/div/div[2]/div/ul/li[1]/a/span[1]
								WebElement suuDriver = driver.findElement(By
										.xpath("//*[@id='videoPost']/div[2]/div[4]/div/div[2]/div/ul/li[1]/a/span[1]"));
								if (suuDriver != null) {
									return suuDriver;
								} else {
									return null;
								}
							}
						});
					} catch (Exception e1) {
						System.out.println("循环卡在【类目二】,下一步挤掉JSAlert弹窗对话框");
						driver.get("http://mp.163.com/index.html#/content?"+"wy="+y);
						Thread.sleep(400);
						driver.switchTo().alert().accept();
						Thread.sleep(tanChuangTiaoGuo);
						continue;
					}
					Thread.sleep(100);
					driver.findElement(By.linkText(leimu2)).click();*/
					Thread.sleep(100);
					/*************突破27字符****************/
					System.out.println("开始突破【字数限制】");
					JavascriptExecutor js= (JavascriptExecutor) driver;
					//*[@id="videoPost"]/div[2]/div[1]
					WebElement editor = driver.findElement(By.xpath("//*[@id='videoPost']/div[2]/div[1]"));
				    js.executeScript(
				            "arguments[0].setAttribute('class','')",
				            editor);
				    /*************突破27字符***************/
				    Thread.sleep(200);
				    
				    
				    
				    /*************描述中含有违规内容，请修改***************/
				  //*[@id="videoPost"]/div[2]/div[2]/div/p[1]/span
				    WebElement weiguiElement = driver.findElement(By.xpath("//*[@id='videoPost']/div[2]/div[2]/div/p[1]/span"));
				    if ("描述中含有违规内容，请修改".equals(weiguiElement.getText())) {
						// 删除第一行
						List<String> listFile = readFile(filepath + "/"+ "乱序" + ".txt", line0);
						System.out.println("还有("+listFile.size()+")个【视频】没有发送");
						writeFile(filepath + "/"+ "乱序" + ".txt", listFile);
						
						driver.get("http://mp.163.com/index.html#/content?"+"wy="+y);
						Thread.sleep(400);
						driver.switchTo().alert().accept();
						Thread.sleep(tanChuangTiaoGuo);
						continue;
					}
				    /*************描述中含有违规内容，请修改***************/
					
				    
				    /*************卡在0.00%不动解决方案***************/
				    WebElement kazhu100 = driver.findElement(By.xpath("//*[@id='videoUp']/div/div[2]/span[2]"));
					if(kazhu100 != null && "0%".equals(kazhu100.getText())){
						driver.get("http://mp.163.com/index.html#/content?"+"wy="+y);
						Thread.sleep(400);
						driver.switchTo().alert().accept();
						Thread.sleep(tanChuangTiaoGuo);
						continue;
					}
				    /*************卡在0.00%不动解决方案***************/
					
					
				  //*[@id="videoPost"]/div[2]/div[2]
				    Thread.sleep(50);
				    /*************顶掉弹窗****************/
				    /*int jumpAlert = 0;
					for(int ee =0;ee<9999;ee++){
						System.out.println("检测到没到100");
						WebElement ddElement = driver.findElement(By.className("radio-img"));
						WebElement removeAlert = driver.findElement(By.className("modal-title"));
						if(removeAlert != null && "提示".equals(removeAlert.getText())){
							driver.get("http://mp.163.com/index.html#/content?"+"wy="+y);
							Thread.sleep(400);
							driver.switchTo().alert().accept();
							jumpAlert = 1;
							break;
						}
						if(ddElement != null){
							break;
						}
						Thread.sleep(1000);
					}
					if(jumpAlert ==1){
						continue;
					}*/
				    /*************顶掉弹窗***************/
					System.out.println("弹窗之下");
					
					
					
					//**************等待时间90秒****************
					try {
						WebDriverWait wait = new WebDriverWait(driver, dengdai);
						wait.until(new ExpectedCondition<WebElement>() {
							@Override
							public WebElement apply(WebDriver d) {
								WebElement ddElement = driver.findElement(By.className("percent"));
								if ("100%".equals(ddElement.getText())) {
									return ddElement;
								} else {
									return null;
								}
							}
						});
					} catch (Exception e1) {
						System.out.println("循环卡在【等待时间90秒】,下一步挤掉JSAlert弹窗对话框");
						driver.get("http://mp.163.com/index.html#/content?"+"wy="+y);
						Thread.sleep(400);
						driver.switchTo().alert().accept();
						Thread.sleep(tanChuangTiaoGuo);
						continue;
					}
					//**************等待时间90秒****************
					
					/*************从100%到卡住图片之间超过自定义秒数，就退出重来***************/
					System.out.println("100%到卡图片之间");
					try {
						(new WebDriverWait(driver, katushijian)).until(new ExpectedCondition<WebElement>() {
							@Override
							public WebElement apply(WebDriver arg0) {
								WebElement kazhu100 = driver.findElement(By.className("radio-img"));
								if (kazhu100 != null) {
									return kazhu100;
								} else {
									return null;
								}
							}
						});
					} catch (Exception e) {
						System.out.println("循环卡在【网易返回12张图片】环节,下一步挤掉JSAlert弹窗对话框");
						driver.get("http://mp.163.com/index.html#/content?"+"wy="+y);
						Thread.sleep(400);
						driver.switchTo().alert().accept();
						Thread.sleep(dengDaiShiMiao);
						continue;
					}
				    /*************从100%到卡住图片之间超过自定义秒数，就退出重来***************/
					
					
					
					//**************解决空白图片被选中****************
					for (int i = 0; i < 9; i++) {
					WebElement tupianClick = driver.findElement(By.xpath(dianjitupian[random.nextInt(8)]));
					WebElement tupianToImg = tupianClick.findElement(By.tagName("input"));
					Thread.sleep(50);
						if(StringUtils.isNotEmpty(tupianToImg.getAttribute("value"))){
							tupianClick.click();
							break;
						}
					}
					//**************解决空白图片被选中****************
					
					System.out.println("视频图片被成功选中");
					
					
				    /*************防止IP探测****************/
					JavascriptExecutor jsIp= (JavascriptExecutor) driver;
					//*[@id="inContent"]/input[6]
					WebElement editorIp = driver.findElement(By.xpath("//*[@id='inContent']/input[6]"));
					String ip = String.valueOf(random.nextInt(125))+"."+String.valueOf(random.nextInt(254))
					+"."+String.valueOf(random.nextInt(254))+"."+String.valueOf(random.nextInt(254));
					jsIp.executeScript(
				            "arguments[0].value=\""+ip+"\"",
				            editorIp);
				    /*************防止IP探测***************/
					
					/*************tanchuAlert_秒内弹窗必须出来***************/
					driver.findElement(By.xpath("//*[@id='videoPost']/div[2]/div[7]/button")).click();
					Thread.sleep(800);
					try {
						(new WebDriverWait(driver, tanchuAlert)).until(new ExpectedCondition<WebElement>() {
							@SuppressWarnings("unused")
							@Override
							public WebElement apply(WebDriver arg0) {
								System.out.println("【弹出确认框】");
								WebElement sDriver = driver.findElement(By.className("js-btn-ok"));
								System.out.println("抓到【js-btn-ok】");
								System.out.println(sDriver.getText());
								if (sDriver != null) {
									return sDriver;
								} else {
									return null;
								}
							}
						});
					} catch (Exception e) {
						System.out.println("循环卡在【tanchuAlert_秒内弹窗必须出来】,下一步挤掉JSAlert弹窗对话框");
						driver.get("http://mp.163.com/index.html#/content?"+"wy="+y);
						Thread.sleep(400);
						driver.switchTo().alert().accept();
						Thread.sleep(dengDaiShiMiao);
						continue;
					}
					/*************tanchuAlert_秒内弹窗必须出来***************/
					
					Thread.sleep(800);
					driver.findElement(By.className("js-btn-ok")).click();
					System.out.println("等待【跳过弹窗】");
					int breakfor = 0;
					for(int ee =0;ee<15;ee++){
						Thread.sleep(800);
						System.out.println("等待跳过弹窗,刷新次数【"+ee+"】");
						String title = driver.getTitle();
						System.out.println("监测点,等待跳过弹窗");
						if(!title.contains("内容管理")&&!title.contains("发布视频")){
							driver.get("http://mp.163.com/index.html#/content?"+"wy="+ee);
							if(ee%2==1){
								driver.navigate().refresh();
							}
						}else if(title.contains("发布视频")&&ee%3==2){
							driver.get("http://mp.163.com/index.html#/content?"+"wy="+ee);
							Thread.sleep(400);
							driver.switchTo().alert().accept();
							Thread.sleep(dengDaiShiMiao);
							breakfor = 1;
						}else if(title.contains("内容管理")){
							break;
						}
						Thread.sleep(400);
					}
					if(breakfor ==1){
						System.out.println("循环卡在【发布视频】,停止后面的循环,视频不删除");
						continue;
					}
					
					// 删除视频第一行
					List<String> listShiPing = readFile(filepath + "/"+ "乱序" + ".txt", line0);
					System.out.println("还有("+listShiPing.size()+")个【视频】没有发送");
					writeFile(filepath + "/"+ "乱序" + ".txt", listShiPing);
					if (y >= 5) {
						driver.manage().deleteAllCookies();
						System.out.println("本账号循环完毕进入【下一个账号】环节");
						break;
					}
				}
				// 删除帐号第一行
					List<String> listZhangHao = readFile(
							zhangHaofileSource + zhangHaoLeixing + "/" + dateString + ".txt", line2);
				System.out.println("------------------------------------------");
				System.out.println("还有("+listZhangHao.size()+")个【帐号】没有传输完");
				writeFile(zhangHaofileSource + zhangHaoLeixing + "/" + dateString + ".txt", listZhangHao);
				System.out.println(s1[0]+"6个视频成功上传完毕");
				System.out.println("---------------end---------------------");
				}
			}
		} catch (Exception e) {
			File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
		    try {
		    	Date ss = new Date(); 
		    	String time0 = format0.format(ss.getTime());
		    	String time1 = format1.format(ss.getTime());
				FileUtils.copyFile(srcFile, new File("d:\\网易号故障截图\\" + time1 + "\\" + time0 + ".png"));
		    } catch (IOException e1) {
		      e1.printStackTrace();
		    }
		    Runtime.getRuntime().exec("taskkill /F /IM chrome.exe");
			Runtime.getRuntime().exec("taskkill /F /IM chromedriver.exe");
			
		}
		 driver.quit();
	};




	// 方法1：

	public void method1() {
		FileWriter fw = null;
		try {
			// 如果文件存在，则追加内容；如果文件不存在，则创建文件
			File f = new File("E:\\dd.txt");
			fw = new FileWriter(f, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		PrintWriter pw = new PrintWriter(fw);
		pw.println("追加内容");
		pw.flush();
		try {
			fw.flush();
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 方法2：

	public static void method2(String file, String conent) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
			out.write(conent + "\r\n");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	// 方法3：

	public static void method3(String fileName, String content) {
		try {
			// 打开一个随机访问文件流，按读写方式
			RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
			// 文件长度，字节数
			long fileLength = randomFile.length();
			// 将写文件指针移到文件尾。
			randomFile.seek(fileLength);
			randomFile.writeBytes(content + "\r\n");
			randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static List<String> readFileAll(String filePath) throws IOException {
		File file = new File(filePath);
		BufferedReader reader = null;
		List<String> list = new ArrayList<String>();
		reader = new BufferedReader(new FileReader(file));
		String text = reader.readLine();
		while (text != null) {
			list.add(text.trim() + "\r\n");
			text = reader.readLine();
		}
		reader.close();
		return list;
	}

	public static List<String> readFile(String filePath, String str) throws IOException {
		File file = new File(filePath);
		BufferedReader reader = null;
		List<String> list = new ArrayList<String>();
		reader = new BufferedReader(new FileReader(file));
		String text = reader.readLine();
		while (text != null) {
			if (!text.trim().equals(str)) {
				list.add(text.trim() + "\r\n");
			}
			text = reader.readLine();
		}
		reader.close();
		return list;
	}

	public static void writeFile(String filePath, List<String> list) throws IOException {
		File file = new File(filePath);
		if (!file.exists()) {
			file.createNewFile();
		}
		FileOutputStream outputStream = new FileOutputStream(file);
		for (String s : list) {
			outputStream.write(s.getBytes());
		}
		outputStream.close();
	}
	
	public static void writeFileRN(String filePath, List<String> list) throws IOException {
		File file = new File(filePath);
		if (!file.exists()) {
			file.createNewFile();
		}
		FileOutputStream outputStream = new FileOutputStream(file);
		for (String s : list) {
			outputStream.write((s+"\r\n").getBytes());
		}
		outputStream.close();
	}
	

	private static boolean getNetworkState(String ip) {
		Runtime runtime = Runtime.getRuntime();
		try {
			Process process = runtime.exec("ping " + ip);
			InputStream iStream = process.getInputStream();
			InputStreamReader iSReader = new InputStreamReader(iStream, "UTF-8");
			BufferedReader bReader = new BufferedReader(iSReader);
			String line = null;
			StringBuffer sb = new StringBuffer();
			while ((line = bReader.readLine()) != null) {
				sb.append(line);
			}
			iStream.close();
			iSReader.close();
			bReader.close();
			String result = new String(sb.toString().getBytes("UTF-8"));
			if (!StringUtils.isBlank(result)) {
				if (result.indexOf("TTL") > 0 || result.indexOf("ttl") > 0) {
					return true;//有网
				} else {
					return true;//没有网
				}
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;//没有网
	
	}

}
